<?php 
	namespace Admin\Controller;

	class ModuleController extends AdminController
	{
		// 查看
		public function index()
		{
			$module = M('module');
			$book = M('mbook');


			$title = $module->select();
			$mbook = $book->select();

			$mk = $book->field('mid')->select();

			$array = array();
			foreach ($mk as $value) {
				foreach ($value as $vl) {
					$array[] = $vl;
				}
			}
			// echo "<pre>";
			// var_dump($array);
			// foreach
			$this->assign('title',$title);
			$this->assign('mbook',$mbook);
			$this->assign('array',$array);
			$this->display();
		}

		// 执行添加
		public function doadd()
		{
			$module = M('module');

			// 判断是否为空
			if(empty(I()['module'])){
				$this->success('版块名不能为空');
				exit;
			}

			if($module->add(I())){
				$this->success('版块添加成功',U('Module/index'));
			}else{
				$this->success('版块添加失败');
			}

		
		}

		// 删除
		public function del()
		{
			$module = M('module');

			$mbook = M('mbook');

			$m = $mbook->field('mid')->select();
			
			// 判断版块是否可删
			foreach($m as $v){
				if(I()['id'] == $v['mid']){
					$this->error('版块下有内容哦，请删除图书再试');
					exit;
				}
			}														

			if($module->delete(I()['id']) > 0){
				$this->success('版块删除成功',U('Module/index'));
			}else{
				$this->success('版块删除失败');
			}
		}

		// 修改的内容
		public function edit()
		{
			$module = M('module');

			$edit = $module->where(I()['id'])->find();
			// var_dump(I());
			$this->assign('vo',$edit);
			$this->display();
		}

		// 执行修改的内容
		public function doedit()
		{
			$module = M('module');
			// var_dump(I());
			if($module->save(I()) > 0){
				$this->success('修改成功',U('Module/index'));
			}else{
				$this->success('修改失败');
			}
		}

	}
	